package com.workstudy.wstudy.service.impl;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.common.utils.DateUtils;
import com.workstudy.wstudy.domain.Constants;
import com.workstudy.wstudy.domain.WstPostUser;
import com.workstudy.wstudy.mapper.WstPostUserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.wstudy.mapper.WstApplyMapper;
import com.workstudy.wstudy.domain.WstApply;
import com.workstudy.wstudy.service.IWstApplyService;
import com.workstudy.common.core.text.Convert;

/**
 * 申请Service业务层处理
 * 
 * @author sun
 * @date 2021-04-05
 */
@Service
public class WstApplyServiceImpl implements IWstApplyService 
{
    @Autowired
    private WstApplyMapper wstApplyMapper;

    @Autowired
    private WstPostUserMapper wstPostUserMapper;

    /**
     * 查询申请
     * 
     * @param applyId 申请ID
     * @return 申请
     */
    @Override
    public WstApply selectWstApplyById(Long applyId)
    {
        return wstApplyMapper.selectWstApplyById(applyId);
    }

    /**
     * 查询申请列表
     * 
     * @param wstApply 申请
     * @return 申请
     */
    @Override
    public List<WstApply> selectWstApplyList(WstApply wstApply)
    {
        return wstApplyMapper.selectWstApplyList(wstApply);
    }

    /**
     * 查询申请列表
     *
     * @param wstApply 申请
     * @return 申请
     */
    @Override
    public List<WstApply> selectWstApplyList2(WstApply wstApply)
    {
        return wstApplyMapper.selectWstApplyList2(wstApply);
    }

    /**
     * 新增申请
     * 
     * @param wstApply 申请
     * @return 结果
     */
    @Override
    public int insertWstApply(WstApply wstApply)
    {
        wstApply.setCreateTime(DateUtils.getNowDate());
        return wstApplyMapper.insertWstApply(wstApply);
    }

    /**
     * 修改申请
     * 
     * @param wstApply 申请
     * @return 结果
     */
    @Override
    public int updateWstApply(WstApply wstApply)
    {
        wstApply.setUpdateTime(DateUtils.getNowDate());
        return wstApplyMapper.updateWstApply(wstApply);
    }

    /**
     * 删除申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWstApplyByIds(String ids)
    {
        return wstApplyMapper.deleteWstApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除申请信息
     * 
     * @param applyId 申请ID
     * @return 结果
     */
    @Override
    public int deleteWstApplyById(Long applyId)
    {
        return wstApplyMapper.deleteWstApplyById(applyId);
    }

    @Override
    public boolean studentPost(WstApply apply){
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        apply.setStudentId(user.getUserId());
        WstPostUser postUser = new WstPostUser();
        postUser.setUserId(user.getUserId());
        List<WstPostUser> list =wstPostUserMapper.selectWstPostUserList(postUser);
        for (WstPostUser p:
                list) {
            if (Constants.ApplyStatus.ON.toString().equals(p.getStatus())
                || (apply.getPostId()==p.getPostId())){
                return false;
            }
        }
        return true;
    }
}
